package com.hs.pre.module.perinfo;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hs.common.JSONHandle;
import com.hs.pre.model.SortBean;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageUtil;
import com.hs.pre.page.ReverseLoadData;

public class PerInfoAction extends org.apache.struts.actions.DispatchAction{


	
	/*****
	 * 方剂列表
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

		/***
		 * 分类id
		 */
		String id = request.getParameter("sortId");
		 try {
			 if(id != null && !(id=id.trim()).equals("") && !id.equals("null"))
			 {
				 PageParam pageParam = new PageParam(request);
				 /**
				  * 默认为15条
				  */
				 pageParam.setPageSize(15);
				 pageParam.setBasePath("/pre/perInfo.do?perInfo=list&sortId="+id);
				 
				 PerInfoDAO perInfoDAO = new PerInfoDAO();
				 
				 PageUtil pageUtil = perInfoDAO.getPerInfoList(new Integer(id),pageParam);
			
				 request.setAttribute("pageUtil", pageUtil);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapping.findForward("list");

	}
	
	/*****
	 * 未分类方剂列表
	 */
	public ActionForward notSortlist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/***
		 * 分类id
		 */
		String id = request.getParameter("id");
		 try {
			 if(id != null && !(id=id.trim()).equals(""))
			 {
				 PageParam pageParam = new PageParam(request);
				 /**
				  * 默认为15条
				  */
				 pageParam.setPageSize(15);
				 pageParam.setBasePath("/pre/perInfo.do?perInfo=notSortlist&id="+id);
				 
				 PerInfoDAO perInfoDAO = new PerInfoDAO();
				 
				 PageUtil pageUtil = perInfoDAO.getNotSortPerInfoList(pageParam);
			
				 request.setAttribute("pageUtil", pageUtil);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mapping.findForward("notSortList");
	}
	
	/**
	 * 解除方剂与分类关系
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * Relations
	 */
	public ActionForward delSortPreRel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] preId = request.getParameterValues("preId");
		String id = request.getParameter("sortId");
		if(preId != null && preId.length>0)
		{
			if(id != null && !(id=id.trim()).equals(""))
			{
				List list = new ArrayList();
				int size = preId.length;
				for(int i=0 ;i<size; i++)
				{
					Object[] va= new Object[2];
					va[0]=id;
					va[1]=Long.parseLong(preId[i]);
					list.add(va);
				}
				 PerInfoDAO perInfoDAO = new PerInfoDAO();
				 try {
					perInfoDAO.delSortPreRel(list);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			}
		}
		return new ActionForward("/perInfo.do?perInfo=list&sortId="+id,true);
	}
	
	/**
	 * 把方剂选入分类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward addSortlist(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] preId = request.getParameterValues("preId");
		String id = request.getParameter("sortId");
		if(preId != null && preId.length>0)
		{
			if(id != null && !(id=id.trim()).equals(""))
			{
//				List va = new ArrayList();
				int size = preId.length;
				StringBuffer st=new StringBuffer("");
				for(int i=0 ;i<size; i++)
				{
//					String[] vaStr = new String[1];
//					vaStr[0]=preId[i];
//					va[i] = preId[i];
//					vaStr[1]=id;
					//va.add(vaStr);
					st.append("'"+preId[i]+"',");
				}
				
				if(!st.equals(""))
					st.deleteCharAt(st.length()-1);
				
				 PerInfoDAO perInfoDAO = new PerInfoDAO();
				 perInfoDAO.addPreSort(st.toString(),id);
				 
			}
		}
		
		return new ActionForward("/perInfo.do?perInfo=list&sortId="+id,true);
	}
}
